
/**
 * Copyright (c) Css Team
 * All rights reserved.
 *
 * This file AddRoleFuncAction.java 
 * http://www.css.com.cn
 */

package com.css.apps.base.role.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.role.model.SRoleFunc;
import com.css.apps.base.user.service.UserHelper;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>
 * 添加角色功能
 * </p>
 * 
 */
@SuppressWarnings("serial")
public class AddRoleFunc extends AdminAction {
	private static Log log = LogFactory.getLog(AddRoleFunc.class);
	private String roleId;
	private String ids;

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(roleId)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			SRole roleObj = QueryCache.get(SRole.class, roleId);
			List<String> newFuncs = StringHelper.isEmpty(ids) ? null : StringHelper.strToList(ids);

			List<String> oldFuncs = roleObj.getRoleFuncList().getListById();
			// new QueryCache("select a.funcId from SRoleFunc a where
			// a.roleId=:roleId").setParameter("roleId", roleId).list();
			List<String> delFuncsList = ListUtil.getFilter(oldFuncs, newFuncs);
			List<String> addFuncsList = ListUtil.getFilter(newFuncs, oldFuncs);
			List<String> delList = null;

			if (delFuncsList != null && delFuncsList.size() > 0) {
				delList = new QueryCache(
						"select a.uuid from SRoleFunc a where a.roleId=:roleId and a.funcId in (:funcId)")
								.setParameter("roleId", roleId).setParameter("funcId", delFuncsList).list();
			}
			List<SRoleFunc> addList = null;
			if (addFuncsList != null && addFuncsList.size() > 0) {
				addList = new ArrayList<SRoleFunc>();
				for (String func : addFuncsList) {
					SRoleFunc item = new SRoleFunc();
					item.setFuncId(func);
					item.setRoleId(roleId);
					addList.add(item);
				}
			}
			tx = new TransactionCache();
			if (delList != null && delList.size() > 0) {
				tx.delete(SRoleFunc.class, delList);
				if (roleObj.getRoleFuncList() != null) {
					roleObj.getRoleFuncList().removeAll();
				}
			}
			if (addList != null && addList.size() > 0) {
				tx.save(addList);
				for (SRoleFunc addfunc : addList) {
					if (roleObj.getRoleFuncList() != null) {
						roleObj.getRoleFuncList().add(addfunc.getFuncId());
					}
				}
			}
			tx.commit();
			// 清空跟此角色直接关联用户的菜单信息缓存
			List<String> userIdList = new QueryCache("select a.userId from SUserRole a where a.roleId=:roleId")
					.setParameter("roleId", roleId).list();
			UserHelper helper = new UserHelper();
			for (String userId : userIdList) {
				helper.clearUserMenuCache(userId);
			}
			// 清空通过岗位跟此角色间接关联的用户菜单信息缓存
			List<String> postIdList = new QueryCache("select a.postId from SPostRole a where a.roleId=:roleId")
					.setParameter("roleId", roleId).list();
			if (postIdList != null && postIdList.size() > 0) {
				List<String> userPostIdList = new QueryCache(
						"select a.userId from SUserPost a where a.postId in(:postIdList)")
								.setParameter("postIdList", postIdList).list();
				for (String userId : userPostIdList) {
					if (!userIdList.contains(userId)) {
						helper.clearUserMenuCache(userId);
					}
				}

			}
			if (delList != null && delList.size() > 0) {
				for (String delId : delList) {
					LogPart lp = new LogPart();
					lp.setOpObjType(SRoleFunc.class.getName());
					lp.setOpObjId(delId);
					lp.setRelObjType(SRole.class.getName());
					lp.setRelObjId(roleId);
					lp.setOpType(LogConstant.LOG_TYPE_DELETE);
					lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lp.setLogData("");
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lp.save();
				}
			}
			if (addList != null && addList.size() > 0) {
				for (SRoleFunc roleFunc : addList) {
					LogPart lp = new LogPart();
					lp.setOpObjType(SRoleFunc.class.getName());
					lp.setOpObjId(roleFunc.getUuid());
					lp.setRelObjType(SRole.class.getName());
					lp.setRelObjId(roleFunc.getRoleId());
					lp.setOpType(LogConstant.LOG_TYPE_ADD);
					lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lp.setLogData(Json.object2json(roleFunc));
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lp.save();
				}
			}

			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
